package com.itany.controller;

import com.github.pagehelper.PageInfo;
import com.itany.constant.StatusConst;
import com.itany.exception.ManagerServiceException;
import com.itany.pojo.ManagerUser;
import com.itany.pojo.Permission;
import com.itany.service.PermissionService;
import com.itany.vo.ActionResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/permission")
public class PermissionController {

    @Autowired
    private PermissionService permissionService;


    @RequestMapping("/findAllPermission")
    public Map<String, Object> findAllPermission(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer rows,
            String moduleName, String permissionName
    ) {
        Map<String, Object> map = new HashMap<String, Object>();
        PageInfo<Permission> info = permissionService.findAllPermission(page, rows, moduleName, permissionName);
        map.put("total", info.getTotal());
        map.put("rows", info.getList());
        return map;
    }

    @RequestMapping("/findAllModulesWithPermission")
    public List<Map<String, Object>> findAllModulesWithPermission() {
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        List<Permission> data = permissionService.findAllModulesWithPermission();
        for (int i = 0; i < data.size(); i++) {
            Permission module = data.get(i);
            Map<String, Object> a = new HashMap<String, Object>();
            list.add(a);
            a.put("id", module.getId());
            a.put("text", module.getName());
            a.put("state", "open");
            a.put("checked", false);

            List<Permission> permissions = module.getPermissions();
            if (permissions != null && !permissions.isEmpty()) {
                List<Map<String, Object>> list2 = new ArrayList<Map<String, Object>>();
                a.put("children", list2);
                for (int j = 0; j < permissions.size(); j++) {
                    Map<String, Object> b = new HashMap<String, Object>();
                    b.put("id", permissions.get(j).getId());
                    b.put("text", permissions.get(j).getName());
                    b.put("state", "open");
                    b.put("checked", false);
                    list2.add(b);
                }
            }

        }
        return list;
    }

    @RequestMapping("/findAllModules")
    public ActionResult findModules() {
        ActionResult result = new ActionResult();
        List<Permission> list = permissionService.findAllModules();
        result.setData(list);
        return result;
    }

    @RequestMapping("/update")
    public ActionResult update(Permission p) {
        ActionResult result = new ActionResult();
        try {
            permissionService.update(p);
        } catch (ManagerServiceException e) {
            result.setStatus(StatusConst.FAIL_RESPONSE);
            result.setMsg(e.getMessage());
        }
        return result;
    }


    //    @RequestMapping("/findAllModulesWithPermissionsByLoginUserId")
//    public List<Map<String, Object>> findAllModulesWithPermissionsByLoginUserId(HttpSession session) {
//        ManagerUser managerUser = (ManagerUser) session.getAttribute(ConstManager.LOGIN_USER);
//
//        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
//        List<Permission> data = permissionService.findAllModulesWithPermissionsByLoginUserId(managerUser.getId());
//        return list;
//    }

    /**
     * 动态主页 查询登录用户所拥有的模块及对应权限和url
     * @param session
     * @return
     */
    @RequestMapping("/findAllModulesWithPermissionsByLoginUserId")
    public ActionResult findAllModulesWithPermissionsByLoginUserId(HttpSession session) {
        ActionResult result = new ActionResult();
        ManagerUser managerUser = (ManagerUser) session.getAttribute(StatusConst.LOGIN_USER);
        if (managerUser == null) {
            result.setMsg("没登录");
            result.setStatus(StatusConst.FAIL_RESPONSE);
            return result;
        }
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        List<Permission> data = permissionService.findAllModulesWithPermissionsByLoginUserId(managerUser.getId());

//        "menuid": "10",
//                "icon": "icon-sys",
//                "menuname": "用户/订单管理",
//                "menus": [{
//            "menuid": "11",
//                    "menuname": "用户管理",
//                    "icon": "icon-nav",
//                    "url": "/manager/showlist"
//        }, {

        data.forEach(module -> {
            Map<String, Object> m = new HashMap<>();
            list.add(m);
            m.put("menuid", module.getId());
            m.put("menuname", module.getName());
            m.put("icon", "icon-sys");

            List<Map<String, Object>> pers = new ArrayList<Map<String, Object>>();
            m.put("menus", pers);
            module.getPermissions().forEach(per -> {
                Map<String, Object> p = new HashMap<>();
                pers.add(p);
                p.put("menuid", per.getId());
                p.put("menuname", per.getName());
                p.put("icon", "icon-sys");
                p.put("url","/manager"+per.getUrl());
            });


        });


        result.setData(list);
        return result;
    }

}
